﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_AdminHome : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["admin"] == null)
            {
                Response.Redirect("../Default.aspx");
            }
            else
            {
                if (Session["InitType"] != null)
                {
                    switch (Session["InitType"].ToString())
                    {
                        case "信息公告编辑":
                            NoticeManage();
                            break;
                        case "学院新闻编辑":
                            CollegeNewsManage();
                            break;
                        case "独立学院动态编辑":
                            IndependentInstituteDynamicManage();
                            break;
                        case "校园动态编辑":
                            CampusDynamicManage();
                            break;
                        case "图片新闻编辑":
                            PictureNewsManage();
                            break;
                        default:
                            break;
                    }
                }
                else 
                {
                    PictureNewsManage();
                }
            }
        }
        else
        { 
        }
    }
    protected void Logoff_Click(object sender, EventArgs e)
    {
        Session.Remove("admin");
        Response.Redirect("../Default.aspx");
    }

    protected void MenuLogoff_Click(object sender, EventArgs e)
    {
        Session.Remove("admin");
        Response.Redirect("../Default.aspx");
    }
    protected void PictureNewsManagement_Click(object sender, EventArgs e)
    {
        Management.Text = "图片新闻列表";
        NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
        NewsListGridView.DataSource = from news in dc.News
                                      join news_to_catalog in dc.NewsToCatalogs
                                      on news.News_id equals news_to_catalog.News_id
                                      where news_to_catalog.Catalog_id.Equals(1)
                                      select new
                                      {
                                          标识 = news.News_id,
                                          标题 = news.Title
                                      };
        NewsListGridView.DataBind();
        dc.Dispose();
    }
    protected void CollegeNewsManagement_Click(object sender, EventArgs e)
    {
        Management.Text = "学院新闻列表";
        NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
        NewsListGridView.DataSource = from news in dc.News
                                      join news_to_catalog in dc.NewsToCatalogs
                                      on news.News_id equals news_to_catalog.News_id
                                      where news_to_catalog.Catalog_id.Equals(2)
                                      select new
                                      {
                                          标识 = news.News_id,
                                          标题 = news.Title
                                      };
        NewsListGridView.DataBind();
        dc.Dispose();
    }
    protected void NoticeManagement_Click(object sender, EventArgs e)
    {
        Management.Text = "信息公告列表";
        NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
        NewsListGridView.DataSource = from news in dc.News
                                      join news_to_catalog in dc.NewsToCatalogs
                                      on news.News_id equals news_to_catalog.News_id
                                      where news_to_catalog.Catalog_id.Equals(3)
                                      select new
                                      {
                                          标识 = news.News_id,
                                          标题 = news.Title
                                      };
        NewsListGridView.DataBind();
        dc.Dispose();
    }
    protected void IndependentInstituteDynamicManagement_Click(object sender, EventArgs e)
    {
        Management.Text = "独立学院动态列表";
        NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
        NewsListGridView.DataSource = from news in dc.News
                                      join news_to_catalog in dc.NewsToCatalogs
                                      on news.News_id equals news_to_catalog.News_id
                                      where news_to_catalog.Catalog_id.Equals(4)
                                      select new
                                      {
                                          标识 = news.News_id,
                                          标题 = news.Title
                                      };
        NewsListGridView.DataBind();
        dc.Dispose();
    }
    protected void CampusDynamicManagement_Click(object sender, EventArgs e)
    {
        Management.Text = "校园动态列表";
        NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
        NewsListGridView.DataSource = from news in dc.News
                                      join news_to_catalog in dc.NewsToCatalogs
                                      on news.News_id equals news_to_catalog.News_id
                                      where news_to_catalog.Catalog_id.Equals(5)
                                      select new
                                      {
                                          标识 = news.News_id,
                                          标题 = news.Title
                                      };
        NewsListGridView.DataBind();
        dc.Dispose();
    }
    protected void NewsListGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        if (Management.Text.Equals("图片新闻列表"))
        {
            Session.Add("OperationType", "Edit");
            Session.Add("News_id", ((Label)NewsListGridView.Rows[e.NewEditIndex].FindControl("News_id")).Text);
            Session.Add("List", Management.Text);
            Session.Add("NewsType", Management.Text.Replace("列表", "编辑"));
            Response.Redirect("PictureNewsEdit.aspx");
        }
        else
        {
            Session.Add("OperationType", "Edit");
            Session.Add("News_id", ((Label)NewsListGridView.Rows[e.NewEditIndex].FindControl("News_id")).Text);
            Session.Add("List", Management.Text);
            Session.Add("NewsType", Management.Text.Replace("列表", "编辑"));
            Response.Redirect("CommonNewsEdit.aspx");
        }
    }
    protected void ChangePassword_Click(object sender, EventArgs e)
    {
        Response.Redirect("ChangePassword.aspx");
    }
    public void CollegeNewsManage()
    {
        Management.Text = "学院新闻列表";
        NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
        NewsListGridView.DataSource = from news in dc.News
                                      join news_to_catalog in dc.NewsToCatalogs
                                      on news.News_id equals news_to_catalog.News_id
                                      where news_to_catalog.Catalog_id.Equals(2)
                                      select new
                                      {
                                          标识 = news.News_id,
                                          标题 = news.Title
                                      };
        NewsListGridView.DataBind();
        dc.Dispose();
    }
    public void PictureNewsManage()
    {
        Management.Text = "图片新闻列表";
        NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
        NewsListGridView.DataSource = from news in dc.News
                                      join news_to_catalog in dc.NewsToCatalogs
                                      on news.News_id equals news_to_catalog.News_id
                                      where news_to_catalog.Catalog_id.Equals(1)
                                      select new
                                      {
                                          标识 = news.News_id,
                                          标题 = news.Title
                                      };
        NewsListGridView.DataBind();
        dc.Dispose();
    }
    public void NoticeManage()
    {
        Management.Text = "信息公告列表";
        NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
        NewsListGridView.DataSource = from news in dc.News
                                      join news_to_catalog in dc.NewsToCatalogs
                                      on news.News_id equals news_to_catalog.News_id
                                      where news_to_catalog.Catalog_id.Equals(3)
                                      select new
                                      {
                                          标识 = news.News_id,
                                          标题 = news.Title
                                      };
        NewsListGridView.DataBind();
        dc.Dispose();
    }
    public void IndependentInstituteDynamicManage() 
    {
        Management.Text = "独立学院动态列表";
        NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
        NewsListGridView.DataSource = from news in dc.News
                                      join news_to_catalog in dc.NewsToCatalogs
                                      on news.News_id equals news_to_catalog.News_id
                                      where news_to_catalog.Catalog_id.Equals(4)
                                      select new
                                      {
                                          标识 = news.News_id,
                                          标题 = news.Title
                                      };
        NewsListGridView.DataBind();
        dc.Dispose();
    }
    public void CampusDynamicManage()
    {
        Management.Text = "校园动态列表";
        NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
        NewsListGridView.DataSource = from news in dc.News
                                      join news_to_catalog in dc.NewsToCatalogs
                                      on news.News_id equals news_to_catalog.News_id
                                      where news_to_catalog.Catalog_id.Equals(5)
                                      select new
                                      {
                                          标识 = news.News_id,
                                          标题 = news.Title
                                      };
        NewsListGridView.DataBind();
        dc.Dispose();
    }
    protected void New_Button_Click(object sender, EventArgs e)
    {
        Session.Add("OperationType", "New");
        Session.Add("List", Management.Text);
        Session.Add("NewsType", Management.Text.Replace("列表", "编辑"));
        if (Management.Text.Equals("图片新闻列表"))
        {
            Response.Redirect("PictureNewsEdit.aspx");
        }
        else
        {
            Response.Redirect("CommonNewsEdit.aspx");
        }
    }
    protected void NewsListGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //JavaScript.JSDialog.Alert(this, Convert.ToInt32(((Label)NewsListGridView.Rows[e.RowIndex].FindControl("News_id")).Text).ToString());

        int news_id = Convert.ToInt32(((Label)NewsListGridView.Rows[e.RowIndex].FindControl("News_id")).Text);
        try
        {
            if (Management.Text.Equals("图片新闻列表"))
            {
                NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
                var delete_picture = from picture in dc.Pictures
                                     where picture.News_id.Equals(news_id)
                                     select picture;

                try
                {
                    dc.Pictures.DeleteAllOnSubmit(delete_picture);
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                }

                var delete_relation = from relation in dc.NewsToCatalogs
                                      where relation.News_id.Equals(news_id)
                                      select relation;
                try
                {
                    dc.NewsToCatalogs.DeleteAllOnSubmit(delete_relation);
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                }

                var delete_news = from news in dc.News
                                  where news.News_id.Equals(news_id)
                                  select news;
                try
                {
                    dc.News.DeleteAllOnSubmit(delete_news);
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                }
                dc.Dispose();

            }
            else
            {
                SQL.SQLHelper.Delete(news_id);
            }
        }
        catch(Exception ex)
        {
        }
        switch (Management.Text)
        {
            case "信息公告列表":
                NoticeManage();
                break;
            case "学院新闻列表":
                CollegeNewsManage();
                break;
            case "独立学院动态列表":
                IndependentInstituteDynamicManage();
                break;
            case "校园动态列表":
                CampusDynamicManage();
                break;
            case "图片新闻列表":
                PictureNewsManage();
                break;
        }
    }
}
